package com.nangle.managehost;


import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.nangle.managehost.adapter.NoticeAdapter;
import com.nangle.managehost.adapter.RemindAdapter;
import com.nangle.managehost.dialog.MyDialog;
import com.nangle.managehost.entity.Notice;
import com.nangle.managehost.entity.Remind;
import com.nangle.managehost.fragmet.DeviceFragment;
import com.nangle.managehost.fragmet.DoctorFragment;
import com.nangle.managehost.fragmet.NurseFragment;
import com.nangle.managehost.fragmet.PatientFragment;
import com.nangle.managehost.fragmet.SickBedFragment;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

    private RadioButton radio_ward;
    private RadioButton radio_sickbed;
    private RadioButton radio_doctor;
    private RadioButton radio_nurse;
    private RadioButton radio_device;

    private SickBedFragment sickBedFragment;
    private DoctorFragment doctorFragment;
    private NurseFragment nurseFragment;
    private DeviceFragment deviceFragment;
    private PatientFragment patientFragment;
    protected InputMethodManager inputMethodManager;
    private FrameLayout frameLayout;
    private LinearLayout bottom_layout;

    private LinearLayout extra_button;
    private LinearLayout constants_button;
    private LinearLayout remiand_button;
    private LinearLayout notice_button;
    private LinearLayout broadcast_button;
    private LinearLayout change_button;
    private LinearLayout restart_button;
    private TextView date_show;
    private String format= "yyyy年MM月dd日      HH:mm:ss";
    private static final int TIME_CHANGED = 1000;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case TIME_CHANGED:
                    date_show.setText(getSystemTime(format));
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        inputMethodManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
        initView();
        initEvents();
        initData();
        new TimeThread().start();
        //   registerReceiver(mTimeRefreshReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
        //首先 我们先选定一个

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // unregisterReceiver(mTimeRefreshReceiver);
    }

    //初始化  各种个 View
    private void initView() {
        radio_ward = (RadioButton) findViewById(R.id.radio_ward);
        radio_sickbed = (RadioButton) findViewById(R.id.radio_sickbed);
        radio_doctor = (RadioButton) findViewById(R.id.radio_doctor);
        radio_device = (RadioButton) findViewById(R.id.radio_device);
        radio_nurse = (RadioButton) findViewById(R.id.radio_nurse);
        frameLayout = (FrameLayout) findViewById(R.id.fragment_container);
        extra_button = (LinearLayout) findViewById(R.id.extrabed_button);
        extra_button.setOnClickListener(this);
        constants_button = (LinearLayout) findViewById(R.id.contacts_button);
        constants_button.setOnClickListener(this);
        remiand_button = (LinearLayout) findViewById(R.id.remind_button);
        remiand_button.setOnClickListener(this);
        notice_button = (LinearLayout) findViewById(R.id.notice_button);
        notice_button.setOnClickListener(this);
        broadcast_button = (LinearLayout) findViewById(R.id.broadcast_button);
        broadcast_button.setOnClickListener(this);
        change_button = (LinearLayout) findViewById(R.id.changebed_button);
        restart_button = (LinearLayout) findViewById(R.id.restart_button);
        date_show = (TextView) findViewById(R.id.main_date);
        restart_button.setVisibility(View.GONE);
        restart_button.setOnClickListener(this);
        change_button.setVisibility(View.GONE);

    }


    //初始化 监听事件
    private void initEvents() {
        radio_ward.setOnClickListener(this);
        radio_sickbed.setOnClickListener(this);
        radio_doctor.setOnClickListener(this);
        radio_device.setOnClickListener(this);
        radio_nurse.setOnClickListener(this);
    }


    private void initData() {
        FragmentTransaction ft;
        ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_container, new PatientFragment());
        ft.commit();
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction t;
        t = getFragmentManager().beginTransaction();
        final LayoutInflater inflater = LayoutInflater.from(this);
        switch (v.getId()) {
            case R.id.radio_ward:
                t.replace(R.id.fragment_container, new PatientFragment());
                initButton(6);
                t.commit();
                break;
            case R.id.radio_sickbed:
                t.replace(R.id.fragment_container, new SickBedFragment());

                initButton(4);
                t.commit();
                break;
            case R.id.radio_doctor:
                t.replace(R.id.fragment_container, new DoctorFragment());
                initButton(3);
                t.commit();
                break;
            case R.id.radio_nurse:
                t.replace(R.id.fragment_container, new NurseFragment());
                initButton(3);
                t.commit();
                break;
            case R.id.radio_device:
                t.replace(R.id.fragment_container, new DeviceFragment());
                initButton(1);
                t.commit();
                break;
            case R.id.extrabed_button:
                showAddBedDialog();
                break;
            case R.id.restart_button:
                showFailDialog("重启失败，请检查网络！");
                break;
            case R.id.notice_button:
                showNoticeDialog();
                break;
            case R.id.remind_button:
                showRemindDialog();
                break;
            case R.id.broadcast_button:
                showBroadcast("广播失败，请检查网络！");
                break;
            case R.id.contacts_button:
                showFailDialog("获取通讯录失败，请检查网络！");
                break;
            default:
                break;
        }
    }

    private void initButton(int type) {
        switch (type) {
            case 1:
                restart_button.setVisibility(View.VISIBLE);
                extra_button.setVisibility(View.GONE);
                constants_button.setVisibility(View.GONE);
                remiand_button.setVisibility(View.GONE);
                notice_button.setVisibility(View.GONE);
                broadcast_button.setVisibility(View.GONE);
                change_button.setVisibility(View.GONE);
                break;
            case 3:
                restart_button.setVisibility(View.GONE);
                extra_button.setVisibility(View.GONE);
                constants_button.setVisibility(View.VISIBLE);
                remiand_button.setVisibility(View.VISIBLE);
                notice_button.setVisibility(View.VISIBLE);
                broadcast_button.setVisibility(View.GONE);
                change_button.setVisibility(View.GONE);
                break;
            case 4:
                restart_button.setVisibility(View.GONE);
                extra_button.setVisibility(View.GONE);
                constants_button.setVisibility(View.VISIBLE);
                remiand_button.setVisibility(View.VISIBLE);
                notice_button.setVisibility(View.VISIBLE);
                broadcast_button.setVisibility(View.VISIBLE);
                change_button.setVisibility(View.GONE);
                break;
            case 6:
                restart_button.setVisibility(View.GONE);
                extra_button.setVisibility(View.VISIBLE);
                constants_button.setVisibility(View.VISIBLE);
                remiand_button.setVisibility(View.VISIBLE);
                notice_button.setVisibility(View.VISIBLE);
                broadcast_button.setVisibility(View.VISIBLE);
                change_button.setVisibility(View.GONE);
                break;

        }
    }

    //广播
    private BroadcastReceiver mTimeRefreshReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
                date_show.setText(getSystemTime(format));
            }
        }
    };

    //获取时间
    private CharSequence getSystemTime(String timeFormat) {

        String date = new SimpleDateFormat(timeFormat).format(System.currentTimeMillis());
        long sysTime = System.currentTimeMillis();
        return date;
    }
    public class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = TIME_CHANGED;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }
    private void showAddBedDialog(){
        View view = getLayoutInflater().inflate(R.layout.addbed_dialog, null);
        final Button btnAddBed = (Button)view.findViewById(R.id.btn_add_bed);
        final ImageView ivClose = (ImageView)view.findViewById(R.id.iv_close);
        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);
        final EditText etName = (EditText)view.findViewById(R.id.et_name);
        final Spinner spSex = (Spinner)view.findViewById(R.id.sp_sex);
        final Spinner spBedNumber = (Spinner)view.findViewById(R.id.sp_bednumber);

        hideSoftKeyboard(etName);
        builder.setCancelable(false);
        builder.show();
        btnAddBed.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String bednumber = (String)spBedNumber.getSelectedItem();
                String str = String.format("确认添加%s床？",bednumber);
                showSelectDialog(str);

            }
        });
        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });
    }
    protected void hideSoftKeyboard(EditText edit) {
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            boolean isOpen = inputMethodManager.isActive();
            if (isOpen)
                inputMethodManager.hideSoftInputFromWindow(edit.getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    private void showBroadcast(String message){
        View view = getLayoutInflater().inflate(R.layout.layout_broadcast, null);
        final TextView tvContent = (TextView)view.findViewById(R.id.tv_broadcast);
        tvContent.setText(message);

        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);

        builder.setCancelable(true);
        builder.show();


    }
    private void showSelectDialog(String message){
        View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
        final TextView tvContent = (TextView)view.findViewById(R.id.tv_dialog_content);
        tvContent.setText(message);
        final TextView btnYes = (TextView) view.findViewById(R.id.f_quchecbutton_btn_queding);
        final TextView btlNo = (TextView) view.findViewById(R.id.f_quchecbutton_btn_quxiao);
        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);

        builder.setCancelable(false);
        builder.show();
        //设置对话框显示的View
        //点击确定是的监听
        btnYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                showFailDialog("加床失败，请检查网络！");
                builder.cancel();
            }
        });


        btlNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });

    }
    private void showFailDialog(String message){
        View view = getLayoutInflater().inflate(R.layout.add_bed_fail_dialog, null);
        TextView tvContent = (TextView)view.findViewById(R.id.tv_dialog_content);
        tvContent.setText(message);
        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);
        builder.setCancelable(true);
        builder.show();
    }
    private void showNoticeDialog(){
        String timeFormat = "yyyy年MM月dd日";
        View view = getLayoutInflater().inflate(R.layout.notice_dialog, null);
        ImageView ivCloseNotice = (ImageView)view.findViewById(R.id.iv_close_notice);
        ListView lvNotice = (ListView)view.findViewById(R.id.notice_list);
        List<Notice> noticeList = new ArrayList<>();
        Notice notice = new Notice();
        notice.setContent(String.format("患者王小如，请于%s,到%d楼%d层进行抽血检查",getSystemTime(timeFormat),5,3));
        notice.setType(0);
        Notice notice2 = new Notice();
        notice2.setContent(String.format("患者刘晓彤，请于%s,到%d楼%d层进行抽血检查",getSystemTime(timeFormat),5,3));
        notice2.setType(1);
        Notice notice3 = new Notice();
        notice3.setContent(String.format("患者将仍然，请于%s,到%d楼%d层进行抽血检查",getSystemTime(timeFormat),5,3));
        notice3.setType(0);
        Notice notice4 = new Notice();
        notice4.setContent(String.format("患者李梦琪，请于%s,到%d楼%d层进行抽血检查",getSystemTime(timeFormat),5,3));
        notice4.setType(0);
        noticeList.add(notice);
        noticeList.add(notice2);
        noticeList.add(notice3);
        noticeList.add(notice4);
        NoticeAdapter noticeAdapter = new NoticeAdapter(MainActivity.this,noticeList);
        lvNotice.setAdapter(noticeAdapter);
        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);
        builder.setCancelable(false);
        builder.show();
        ivCloseNotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });
    }
    private void showRemindDialog(){
        String timeFormat = "yyyy-MM-dd";
        View view = getLayoutInflater().inflate(R.layout.remind_dialog, null);
        ImageView ivCloseRemind = (ImageView)view.findViewById(R.id.iv_close_remind);
        ListView lvRemind = (ListView)view.findViewById(R.id.remind_list);
        List<Remind> reminds = new ArrayList<>();
        Remind remind = new Remind();
        remind.setContent("通知：原定于今天早上8:00的例会，延后一小时，请相互通知");
        remind.setTime(String.format("%s",getSystemTime(timeFormat)));
        Remind remind2 = new Remind();
        remind2.setContent("通知：下午进行大扫除，请大家积极配合");
        remind2.setTime(String.format("%s",getSystemTime(timeFormat)));
        Remind remind3 = new Remind();
        remind3.setContent("患通知：院长专题讲座将于下午13:00在阶梯教室进行，请全院同志参加");
        remind3.setTime("2017-03-23");
        Remind remind4 = new Remind();
        remind4.setContent("通知：原定于今天早上8:00的例会，延后一小时，请相互通知");
        remind4.setTime("2017-03-23");
        reminds.add(remind);
        reminds.add(remind2);
        reminds.add(remind3);
        reminds.add(remind4);

        RemindAdapter remindAdapter = new RemindAdapter(MainActivity.this,reminds);
        lvRemind.setAdapter(remindAdapter);
        final MyDialog builder = new MyDialog(MainActivity.this,0,0,view,R.style.DialogTheme);
        builder.setCancelable(false);
        builder.show();
        ivCloseRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });
    }
}
